package com.mojo.junit5;

import com.mojo.junit4.UserService;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
 * 整个JUnit5
 * <p>
 * 1、引入依赖spring-test,junit-jupiter
 * 2、测试类使用@SpringJUnitConfig，加载配置文件
 * 3、使用org.junit.jupiter.api下的Test注解
 * 4、测试,测试方法不再需要标识为public
 *
 * </p>
 *
 * @author Mojo
 */
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
class HelloTestJUnit5 {
    @Autowired
    private UserService userService;

    @Test
    void testJunit5() {
        userService.printUser();
    }

    @BeforeAll
    static void initAll() {
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void init() {
        System.out.println("BeforeEach");
    }

    @Test
    void succeedingTest() {
        System.out.println("succeedingTest");
    }

    @Test
    void failingTest() {
        System.out.println("failingTest");
        //使测试失败
        fail("a failing test");
    }

    @Test
    @Disabled("for demonstration purposes")
    void skippedTest() {
        // not executed
    }

    @Test
    void abortedTest() {
        System.out.println("abortedTest");
        //断言
        assumeTrue("abc".contains("Z"));
        fail("test should have been aborted");
    }

    @AfterEach
    void tearDown() {
        System.out.println("AfterEach");
    }

    @AfterAll
    static void tearDownAll() {
        System.out.println("AfterEach");
    }
}